package com.wwwarehouse.common.custom_widget.switchbutton;

import android.content.Context;
import android.support.v7.widget.AppCompatCheckBox;
import android.util.AttributeSet;

import com.wwwarehouse.common.R;


/**
 * @author : mundane
 * @time : 2017/7/11 10:51
 * @description :
 * @file : SwitchButton.java
 */

public class SwitchCheckBox extends AppCompatCheckBox {
	public SwitchCheckBox(Context context) {
		super(context);
		init();
	}

	public SwitchCheckBox(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public SwitchCheckBox(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		init();
	}

	private void init() {
		setButtonDrawable(null);
		setBackgroundResource(R.drawable.selector_switch_button);
//		setHeight(dp2px(20));
//		setWidth(dp2px(37));
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		int widthMode = MeasureSpec.getMode(widthMeasureSpec);
		if (widthMode != MeasureSpec.EXACTLY) {
			widthMeasureSpec = MeasureSpec.makeMeasureSpec(dp2px(37), MeasureSpec.EXACTLY);
		}

		int heightMode = MeasureSpec.getMode(heightMeasureSpec);
		if (heightMode != MeasureSpec.EXACTLY) {
			heightMeasureSpec = MeasureSpec.makeMeasureSpec(dp2px(20), MeasureSpec.EXACTLY);
		}
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	}

	private int px2dp(float paValue) {
		final float scale = getContext().getResources().getDisplayMetrics().density;
		return (int) (paValue / scale + 0.5f);
	}

	private int dp2px(float dpValue) {
		final float scale = getContext().getResources().getDisplayMetrics().density;
		return (int) (dpValue * scale + 0.5f);
	}

	private int px2sp(float pxValue) {
		final float frontScale = getContext().getResources().getDisplayMetrics().scaledDensity;
		return (int) (pxValue / frontScale + 0.5f);
	}

	private int sp2px(float spValue) {
		final float frontScale = getContext().getResources().getDisplayMetrics().scaledDensity;
		return (int) (spValue * frontScale + 0.5f);
	}
}
